package cn.icanci.basic.commonutils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.MDC;

/**
 * TODO 日志记录和发送模块，发送MQ消息，由 log-parser-service 进行处理
 * @author icanci
 * @date Created in 2021/04/11 15:24
 * @version 1.0
 */
public class LogUtil {
    private static final String MODULE        = "LOGGER_MODULE";
    private static final String CATEGORY      = "LOGGER_CATEGORY";
    private static final String SUB_CATEGORY  = "LOGGER_SUB_CATEGORY";
    private static final String PREFIX_MODULE = "TICKET_";

    private LogUtil() {
    }

    public static void info(Logger logger, String format, Object... objects) {
        logger.info(format, objects);
    }

    public static void warn(Logger logger, String format, Object... objects) {
        logger.warn(format, objects);
    }

    public static void error(Logger logger, String format, Object... objects) {
        logger.error(format, objects);
    }

    public static void warn(Logger logger, String format, Throwable throwable, Object... objects) {
        logger.warn(format, throwable, objects);
    }

    public static void error(Logger logger, String format, Throwable throwable, Object... objects) {
        logger.error(format, throwable, objects);
    }

    public static void init(String module, String category, String subCategory) {
        setModule(module);
        setCategory(category);
        setCategory(subCategory);
    }

    public static void setModule(String module) {
        if (StringUtils.isNotBlank(module) && !StringUtils.equals("null", module)) {
            if (StringUtils.startsWith(module, PREFIX_MODULE)) {
                MDC.put(MODULE, module);
            } else {
                MDC.put(MODULE, PREFIX_MODULE + module);
            }
        } else {
            removeModule();
        }
    }

    public static String getModule() {
        return MDC.get(MODULE);
    }

    public static void setCategory(String category) {
        if (StringUtils.isNotBlank(category) && !StringUtils.equals("null", category)) {
            if (StringUtils.startsWith(category, PREFIX_MODULE)) {
                MDC.put(CATEGORY, category);
            } else {
                MDC.put(CATEGORY, PREFIX_MODULE + category);
            }
        } else {
            removeCategory();
        }
    }

    public static String getCategory() {
        return MDC.get(CATEGORY);
    }

    public static void setSubCategory(String subCategory) {
        if (StringUtils.isNotBlank(subCategory) && !StringUtils.equals("null", subCategory)) {
            if (StringUtils.startsWith(subCategory, PREFIX_MODULE)) {
                MDC.put(SUB_CATEGORY, subCategory);
            } else {
                MDC.put(SUB_CATEGORY, PREFIX_MODULE + subCategory);
            }
        } else {
            removeSubCategory();
        }
    }

    public static String getSubCategory() {
        return MDC.get(SUB_CATEGORY);
    }

    public static void removeAll() {
        removeModule();
        removeCategory();
        removeSubCategory();
    }

    public static void removeSubCategory() {
        MDC.remove(SUB_CATEGORY);
    }

    public static void removeCategory() {
        MDC.remove(CATEGORY);
    }

    public static void removeModule() {
        MDC.remove(MODULE);
    }
}
